var app = getApp(), fetchApi = require("../../../utils/httpPost.js"),common_Js = require("../../../component/common.js"),
formatTime = require("../../../component/common.js")
import Toast from '../../../component/vant/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading:false,
    shopName:'',
    commentContent:'',
    submitState:false,
    logo:'',
    leaderName:'',
    phoneNum:'',
    codeImg:'',
    keyword:'',
    lng:0,
    lat:0,
    cateChildId:0,
    fileList0:[],
    fileList1: [],
    fileList2: [],
    fileList3: [],
    checkboxXieyi:false,
    isiphonex: app.globalData.isiphonex,
    isxiaomi: app.globalData.isxiaomi,
    signImg:'',
    ag_id:0,
    cashMoney:20,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options', options)
    var that = this
    var isLogin = app.getStorageSync("isLogin");
    console.log('isLogin',isLogin)
    
    if (options!=undefined && options.q) {
      var q = decodeURIComponent(options.q) // 获取到二维码原始链接内容
      q = "?" + q.split("?")[1];
      this.data.query = q;
      console.log('q', q)
      options = common_Js.getQueryVariable(q);
    }

    if(options.ag_id){
      that.data.ag_id = parseInt(options.ag_id);
    }
    console.log('that.data.ag_id',that.data.ag_id)
    
    if(isLogin == 1){
      var user_id = app.getStorageSync("user_id");
      if(user_id > 0){
        // app.getUserInfoById(user_id)
      }else{
        wx.navigateTo({
          url: '/pages/login/index?ag_id=' + that.data.ag_id,
        })
      }
    }else{
      wx.navigateTo({
        url: '/pages/login/index?ag_id='+that.data.ag_id,
      })
    }
    this.onHttpInit();
  },
  onShow(options){
    console.log('onShow options',options)
    // if(options.signImg){
    //   this.data.signImg = options.signImg;
    // }
    console.log('onShow this.data.signImg',this.data.signImg)
  },
  /**
   * 获取商家邀请注册的 现金奖励金额
   */
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/mer/cash/role/detail', {
    }, function (res) {
      console.log("获取商家邀请注册的", res)
      console.log('res',res)
      if (res.data.code == 0) {
        if(res.data.data.role.maxMoney > 0){
          console.log('res.data.data.role.maxMoney',res.data.data.role.maxMoney)
          that.data.cashMoney = parseInt(res.data.data.role.maxMoney);
          console.log('cashMoney',that.data.cashMoney)
        }
      }
    }, function () {
    })
  },
  trim: function (t) {
    return t.replace(/(^\s*)|(\s*$)/g, "");
  },
  //获取用户定位授权，地图选点
  getLocation: function () {
    var that = this;
    wx.getSetting({
      success:res=>{
        if(!res.authSetting['scope.userLocation']){
          wx.authorize({ 
            scope: 'scope.userLocation', 
            success:res=>{

            },fail:res=>{
              wx.showModal({
                title: '提示',
                content: '系统需要获取该您当前的定位，请确保您的位置授权已开启',
                showCancel: false,
                success: res=>{
                  if (res.confirm) {
                    wx.openSetting({
    
                    })
                  }
                }
              })
            }
          });
        }
        wx.chooseLocation({ 
          success: res=>{
            console.log(res)
            if(res.address!='' && res.name!=''){
              this.setData({
                address: res.address + res.name,
              })
              that.data.address = res.address + res.name;
              that.data.lng = res.longitude;
              that.data.lat = res.latitude;
            }
          }
        })
      }
    })
  },
  shopNameInput: function (t) {
    this.data.shopName = t.detail || "";
  },
  addressInput(e){
    this.data.address = e.detail || "";
  },
  phoneNumInput(e){
    this.data.phoneNum = e.detail || "";
  },
  leaderNameInput(e){
    this.data.leaderName = e.detail || "";
  },
  keyWordInput(e){
    this.data.keyword = e.detail || "";
  },
  deleteImg0:function(res){
      console.log('删除图片',res)
      this.data.fileList0 = []
      this.setData({
        fileList0:[]
      })
  },
  deleteImg1:function(res){
      console.log('删除图片',res)
      this.data.fileList1 = []
      this.setData({
        fileList1:[]
      })
  },
  deleteImg2:function(res){
      console.log('删除图片',res)
      this.data.fileList2 = []
      this.setData({
        fileList2:[]
      })
  },
  deleteImg3:function(res){
      console.log('删除图片',res)
      this.data.fileList3 = []
      this.setData({
        fileList3:[]
      })
  },
  afterRead(event) {
    console.log('afterRead',event)
    const { file, name } = event.detail;
    console.log('afterRead name',name)
    console.log('afterRead file',file)
    const fileList = this.data[`fileList${name}`];
    this.setData({ [`fileList${name}`]: fileList.concat(file) });
    console.log('afterRead fileList',fileList)
    if(name == 0){
      this.data.fileList0.concat(file)
      console.log('fileList0',this.data.fileList0)
    } else if(name == 1){
      this.data.fileList1.concat(file)
      console.log('fileList1',this.data.fileList1)
    } else if(name == 2){
      this.data.fileList2.concat(file)
      console.log('fileList2',this.data.fileList2)
    } else if(name == 3){
      this.data.fileList3.concat(file)
      console.log('fileList3',this.data.fileList3)
    }
  },
  oversize() {
    wx.showToast({ title: '文件超出大小限制', icon: 'none' });
  },
  submitOp:function(e){
    var that = this;
    if(that.data.shopName == '' || that.data.shopName == undefined){
      Toast.fail("请输入商家名称");
      return ;
    }

    if(that.data.address == '' || that.data.address == undefined){
      Toast.fail("请选择商家地址");
      return ;
    }

    if(that.data.lng == 0 || that.data.lat == 0){
      Toast.fail("请选择商家地址");
      return ;
    }

    if(that.data.leaderName == '' || that.data.leaderName == undefined){
      Toast.fail("请输入姓名");
      return ;
    }

    if(that.data.phoneNum == '' || that.data.phoneNum == undefined){
      Toast.fail("请输入手机号");
      return ;
    }

    console.log('that.data.fileList2.length',that.data.fileList2)

    if(that.data.fileList1.length  == 0){
      Toast.fail("请上传营业执照");
      return ;
    }

    if(that.data.fileList2.length  == 0){
      Toast.fail("请上传身份证正面");
      return ;
    }

    if(that.data.fileList3.length  == 0){
      Toast.fail("请上传身份证反面");
      return ;
    }

    if(this.data.signImg == ''){
      Toast.fail("请签写电子签名");
      return ;
    }

    if(this.data.checkboxXieyi == false){
      Toast.fail("请阅读并同意用户协议");
      return ;
    }

    that.setData({ submitting : true})
    fetchApi.httpPost('/mer/register', {
      leaderName: that.data.leaderName,
      shopName: that.data.shopName,
      address: that.data.address,
      keyword: that.data.keyword,
      phoneNum: that.data.phoneNum,
      lng:that.data.lng,
      lat:that.data.lat,
      ag_id:that.data.ag_id,
    }, function (res) {
      console.log('res', res)
      if (res.data.code == 0) {
        var mer_id = res.data.data;
        if (mer_id > 0 && that.data.fileList0.length > 0) {
          for (var index in that.data.fileList0) {
            wx.uploadFile({
              url: fetchApi.getPlatformUrl() + '/mer/uploadImg',
              filePath: that.data.fileList0[index].url,
              name: 'file',
              formData: { mer_id: mer_id, type : 0 },
              success: function (res) {
                console.log('uploadFile success res', res)
              }, fail: function (res) {
                console.log('uploadFile fail res', res)
              }
            })
          }
        }
          
        if (mer_id > 0 && that.data.fileList1.length > 0) {
          for (var index in that.data.fileList1) {
            wx.uploadFile({
              url: fetchApi.getPlatformUrl() + '/mer/uploadImg',
              filePath: that.data.fileList1[index].url,
              name: 'file',
              formData: { mer_id: mer_id , type : 1},
              success: function (res) {
                console.log('uploadFile success res', res)
              }, fail: function (res) {
                console.log('uploadFile fail res', res)
              }
            })
          }
        }

        if (mer_id > 0 && that.data.fileList2.length > 0) {
          for (var index in that.data.fileList2) {
            wx.uploadFile({
              url: fetchApi.getPlatformUrl() + '/mer/uploadImg',
              filePath: that.data.fileList2[index].url,
              name: 'file',
              formData: { mer_id: mer_id, type : 2 },
              success: function (res) {
                console.log('uploadFile success res', res)
              }, fail: function (res) {
                console.log('uploadFile fail res', res)
              }
            })
          }
        }

        
        if (mer_id > 0 && that.data.fileList3.length > 0) {
          for (var index in that.data.fileList3) {
            wx.uploadFile({
              url: fetchApi.getPlatformUrl() + '/mer/uploadImg',
              filePath: that.data.fileList3[index].url,
              name: 'file',
              formData: { mer_id: mer_id, type : 3 },
              success: function (res) {
                console.log('uploadFile success res', res)
              }, fail: function (res) {
                console.log('uploadFile fail res', res)
              }
            })
          }
        }

        if(that.data.signImg != ''){
          wx.uploadFile({
            url: fetchApi.getPlatformUrl() + '/mer/uploadImg',
            filePath: that.data.signImg,
            name: 'file',
            formData: { mer_id: mer_id, type : 4 },
            success: function (res) {
              console.log('uploadFile success res', res)
            }, fail: function (res) {
              console.log('uploadFile fail res', res)
            }
          })
        }

        Toast.success(res.data.msg);
        setTimeout(function(){
          wx.switchTab({
            url: '/pages/home/home',
          })
        },1000)

      } else {
        Toast.fail(res.data.msg);
      }
    },function(){})
  },
  onChangeCheckBox(e){
    console.log('onChangeCheckBox',e)
    this.data.checkboxXieyi = e.detail
    this.setData({
      checkboxXieyi: e.detail
    })
  },
  goNotice1(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/notice/detail/index?is_sign=1&id=' + id,
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    var user_id = app.getStorageSync("user_id");
    console.log( "转发成功 user_id",user_id)
    return {
      title: "快来邀请商家入驻平台吧",
      path: '/pagesMer/pages/register/index?ag_id='+user_id,
      success: function (res) {
        console.log(res, "转发成功")
      },
      fail: function (res) {
        console.log(res, "转发失败")
      }
    }
  },   
})